'''
Created on Jul 7, 2010

@author: Behrooz Nobakht
'''
from amscms.ams.models import AdTagExploreHistory
import re
from amscms.core.utils.request import meta_netloc, meta_remote_ip

class AdTagExploreMiddleware:
    
    def process_request(self, request):
        if re.search("ads/tag/explore/\\d+$", request.path) is None:
            return None
        result = re.search("(?<=/explore/)\\d+", request.path)
        term_id = result.group(0)
        if term_id is None or len(term_id) == 0:
            return None
        referer = meta_netloc(request)
        viewer_ip = meta_remote_ip(request)
        AdTagExploreHistory.objects.add_history(request.user, term_id, referer, viewer_ip)
        return None
        
        
 
